<?php
/**
 * model基类扩展
 * @author chendanming@star-net.cn
 * @copyright © ktvme. All Rights Reserved
 * @version $Id: BaseModel.php 396 2017-11-30 02:21:37Z panchenxing $
 */


namespace app\core;

use think\Config;
use think\Model;
use think\Cache;

class BaseModel extends Model {
    protected $redis  = null;
    protected $prefix = "";
    protected $expire = "";

    public function __construct($data = []) {
        parent::__construct($data);
        $this->redis  = Cache::store('redis')->handler();
        $redisConfig  = Config::get('cache.default');
        $this->prefix = $redisConfig['prefix'];
        $this->expire = $redisConfig['expire'];
    }

    public function getAndSyncRedis($condition, $redisKey) {

        $redisRet = $this->redis->HGETALL($this->prefix . $redisKey);

        if($redisRet) {
            return $redisRet;
        }

        $info = $this->get($condition);
        $info && $info = $info->toArray();

        if($info) {
            //更新缓存
            $this->redis->hMSet($this->prefix . $redisKey, $info);
            $this->redis->EXPIRE($this->prefix . $redisKey, $this->expire);

            return $info;
        }

        return false;
    }

    public function getPkByIndexAndSyncRedis($condition, $index, $redisKey) {
        $redisRet = $this->redis->SMEMBERS($this->prefix . $redisKey);

        if($redisRet) {
            return $redisRet[0];
        }

        $info = $this->get($condition);
        $info && $info = $info->toArray();

        if($info) {
            //更新缓存
            $this->redis->SADD($this->prefix . $redisKey, $info[$index]);
            $this->redis->EXPIRE($this->prefix . $redisKey, $this->expire);

            return $info[$index];
        }

        return false;
    }

    public function insertGetIdAndSyncRedis($params, $primaryKeyName, $redisKey) {
        if(empty($params)) {
            return false;
        }
        $ret = $this->insertGetId($params);

        if($ret) {
            //更新缓存
            $params[$primaryKeyName] = $ret;
            $this->redis->hMSet($this->prefix . $redisKey, $params);
            $this->redis->EXPIRE($this->prefix . $redisKey, $this->expire);

            return $ret;
        }

        return false;
    }

    public function deleteAndSyncRedis($condition, $redisKey) {
        $ret = $this->where($condition)->delete();

        if($ret) {
            //更新缓存
            $this->redis->DEL($this->prefix . $redisKey);

            return $ret;
        }

        return false;
    }

    public function updateAndSyncRedis($params, $condition, $redisKey) {
        $ret = $this->save($params, $condition);

        if($ret) {
            //更新缓存
            if($this->redis->EXISTS($this->prefix . $redisKey)) {
                $this->redis->hMSet($this->prefix . $redisKey, $params);
                $this->redis->EXPIRE($this->prefix . $redisKey, $this->expire);
            }

            return $ret;
        }

        return false;
    }

    public function databaseModeltoArray($data) {
        return json_decode(json_encode($data), true);
    }

    /**
     * 更新逻辑删除并删除redis
     * @param $params
     * @param $condition
     * @param $redisKey
     * @return bool|false|int
     */
    public function updateLogicDelAndSyncRedis($params, $condition, $redisKey) {
        $ret = $this->save($params, $condition);

        if($ret) {
            //删除redis
            if($this->redis->EXISTS($this->prefix . $redisKey)) {
                $this->redis->DEL($this->prefix . $redisKey);
            }

            return $ret;
        }

        return false;
    }

    /**
     * 设置过期时间
     * @param $expires
     */
    public function setExpire($expires) {
        return $this->expire = $expires;
    }
}